package cn.edu.ujn.bmis.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cn.edu.ujn.bmis.model.Donator;
import cn.edu.ujn.bmis.service.BookManager;
import cn.edu.ujn.bmis.service.DonatorManager;
import cn.edu.ujn.bmis.tools.Helper;

import com.opensymphony.xwork2.ActionSupport;

@Component("Donator")
@Scope("prototype")
public class DonatorAction extends ActionSupport {

	private static final long serialVersionUID = 536198696679225399L;
	private DonatorManager donatorManager;
	private Donator donator;
	private Helper helper;
	private List<Donator> donatorList = new ArrayList<Donator>();
	private String targetType = "navTab"; // navTab或dialog，用来标记是navTab上的分页还是dialog上的分页
	private int pageNum = 1;
	private int currentPage = 1; // 当前是第几页
	private int totalCount = 0; // 总条数
	private int numPerPage = 5; // 每页显示多少条
	private int pageNumShown = 5; // 页标数字多少个
	private int pageCount = 0; // 总页数
	private String orderField;
	private String orderDirection;
	private String ids;
	private String donatorId;
	private String donatorName;
	private String functionNum;
	private BookManager bookManager;

	/**
	 * 添加捐赠人基本信息
	 */
	public void addDonator() {
		String hql="";
		if(donator!=null){
			hql=" from Donator d where d.donatorId='"+donator.getDonatorId()+"'";
			donatorList=donatorManager.findDonator(hql);
			if(donatorList.size()>0){
				Helper.out("300", "捐赠人编号已存在，请检查！", "", "", "", "");
				return;
			}
		}
		if (donatorManager.addDonator(donator)) {
			Helper.out("200", "捐赠人添加成功", "closeCurrent", "", "", "manageDonator");
			
		} else {
			Helper.out("300", "捐赠人添加失败", "", "", "", "");
		}
	}

	/**
	 * 修改前的查找
	 */
	public String beforeupdateDonator() {
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		String hql = "from Donator d where d.donatorId='" + donatorId + "'";
		donatorList = donatorManager.findDonator(hql);
		if (donatorList.size() > 0) {
			donator = donatorList.get(0);
		}
		return SUCCESS;
	}

	/**
	 * 修改捐赠人信息
	 */
	public void updateDonator() {
		if (donatorManager.update(donator)) {
			Helper.out("200", "捐赠人修改成功", "closeCurrent", "", "", "manageDonator");
		} else {
			Helper.out("300", "捐赠人修改失败", "", "", "", "");
		}
	}

	/**
	 * 刪除捐贈人
	 */
	public void deleteDonator() {
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
			return;
		}
		String[] strArray = null;
		strArray = ids.split(",");
		String hql = null;
		hql = "delete from Donator d where d.donatorId in ('";
		for (int i = 0; i < strArray.length - 1; i++) {
			hql = hql + strArray[i] + "'" + ",'";
		}
		hql = hql + strArray[strArray.length - 1] + "') ";
		if (donatorManager.update(hql)) {
			Helper.out("200", "捐赠人删除成功", "", "", "", "manageDonator");
		} else {
			Helper.out("300", "捐赠人删除失败", "", "", "", "");
		}
	}

//	/**
//	 * 为转向捐赠人管理页面初始化参数
//	 */
//	public String manageDonator() {
//		totalCount = 0;
//		currentPage = 1;
//		numPerPage = 5;
//		findDonator();
//		return "manage";
//	}
	
	public String beforeAddDonator(){
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		return SUCCESS;
	}

	/**
	 * 查询捐赠者信息
	 */
	@SuppressWarnings("unchecked")
	public String findDonator() {
		String hql = "";
		String hql_size="";
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		this.currentPage = this.pageNum;
		if ((donatorId == null || "".equals(donatorId))
				&& (donatorName == null || "".equals(donatorName))) {
			hql = "from Donator";
			hql_size="select count(*) from Donator";
		} else if (!(donatorId == null || "".equals(donatorId))
				&& (donatorName == null || "".equals(donatorName))) {
			hql = "from Donator d where d.donatorId='" + donatorId.trim() + "'";
			hql_size="select count(*) from Donator d where d.donatorId='" + donatorId.trim() + "'";
		} else if ((donatorId == null || "".equals(donatorId))
				&& !(donatorName == null || "".equals(donatorName))) {
			hql = "from Donator d where d.donatorName like'%" + donatorName.trim() + "%'";
			hql_size="select count(*) from Donator d where d.donatorName='" + donatorName.trim() + "'";
		} else if (!(donatorId == null || "".equals(donatorId))
				&& !(donatorName == null || "".equals(donatorName))) {
			hql = "from Donator d where d.donatorName='" + donatorName.trim()
					+ "' and d.donatorId='" + donatorId.trim() + "'";
			hql_size="select count(*) from Donator d where d.donatorName='" + donatorName.trim()
					+ "' and d.donatorId='" + donatorId.trim() + "'";
		}
		//page();
		totalCount = helper.findSize(hql_size);
		pageCount = totalCount % numPerPage == 0 ? totalCount / numPerPage
				: totalCount / numPerPage + 1;
		donatorList = helper.fenYe(hql, this.currentPage, this.numPerPage);
		return SUCCESS;
	}

	public DonatorManager getDonatorManager() {
		return donatorManager;
	}

	@Resource(name = "donatorManager")
	public void setDonatorManager(DonatorManager donatorManager) {
		this.donatorManager = donatorManager;
	}

	public Donator getDonator() {
		return donator;
	}

	public void setDonator(Donator donator) {
		this.donator = donator;
	}

	public List<Donator> getDonatorList() {
		return donatorList;
	}

	public void setDonatorList(List<Donator> donatorList) {
		this.donatorList = donatorList;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage;
	}

	public int getPageNumShown() {
		return pageNumShown;
	}

	public void setPageNumShown(int pageNumShown) {
		this.pageNumShown = pageNumShown;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public String getOrderField() {
		return orderField;
	}

	public void setOrderField(String orderField) {
		this.orderField = orderField;
	}

	public String getOrderDirection() {
		return orderDirection;
	}

	public void setOrderDirection(String orderDirection) {
		this.orderDirection = orderDirection;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getTargetType() {
		return targetType;
	}

	public void setTargetType(String targetType) {
		this.targetType = targetType;
	}

	public Helper getHelper() {
		return helper;
	}

	@Resource(name = "helper")
	public void setHelper(Helper helper) {
		this.helper = helper;
	}

	public String getDonatorId() {
		return donatorId;
	}

	public void setDonatorId(String donatorId) {
		this.donatorId = donatorId;
	}

	public String getDonatorName() {
		return donatorName;
	}

	public void setDonatorName(String donatorName) {
		this.donatorName = donatorName;
	}

	public String getFunctionNum() {
		return functionNum;
	}

	public void setFunctionNum(String functionNum) {
		this.functionNum = functionNum;
	}

	public BookManager getBookManager() {
		return bookManager;
	}
	
	@Resource(name="bookManager")
	public void setBookManager(BookManager bookManager) {
		this.bookManager = bookManager;
	}
	
	
}
